
package com.example.helloandroid;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.media.MediaPlayer;

public class TxalapartaDrawView extends View {  
   private MediaPlayer mp1;
   private MediaPlayer mp2;
  // private boolean jo = true;
    
    public TxalapartaDrawView(Context context) {
        super(context);
        setFocusable(true); //necessary for getting the touch events
              
        this.setBackgroundResource(R.drawable.mahaia);
        
        // audioarentzat objetua sortu
        mp1 = MediaPlayer.create(context, R.raw.txalasample2);
        mp2 = MediaPlayer.create(context, R.raw.txalasample3);
    }
 
    // events when touching the screen
    public boolean onTouchEvent(MotionEvent event) {
        int eventaction = event.getAction(); 
        
        int X = (int)event.getX(); 

        switch (eventaction ) { 

        case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
       	Log.v("josu", "down "+X);
        	//if(jo){
    			if(X>170){
        			mp1.start();
    			}else{
    				mp2.start();
    			}
    		//	jo = false;
    		//}             
            break; 
        } 
        // redraw the canvas
        invalidate(); 
        return true; 
	
    }
}


//
//package com.example.helloandroid;
//
//import android.content.Context;
//import android.graphics.Canvas;
//import android.graphics.Point;
//import android.view.MotionEvent;
//import android.view.View;
//import android.media.MediaPlayer;
//
//public class DrawView extends View {
//   private ColorBall[] colorballs = new ColorBall[2]; // array that holds the balls
//   private int balID = 0; // variable to know what ball is being dragged
//   private MediaPlayer mp1;
//   private MediaPlayer mp2;
//   private boolean jo = true;
//    
//    public DrawView(Context context) {
//        super(context);
//        setFocusable(true); //necessary for getting the touch events
//        
//        // setting the start point for the balls
//        Point point1 = new Point();
//        point1.x = 50;
//        point1.y = 20;
//        Point point2 = new Point();
//        point2.x = 150;
//        point2.y = 20;
//                               
//        // declare each ball with the ColorBall class
//        colorballs[0] = new ColorBall(context,R.drawable.bol_groen, point1);
//        colorballs[1] = new ColorBall(context,R.drawable.bol_rood, point2);
//        
//        this.setBackgroundResource(R.drawable.mahaia);
//        
//        // audioarentzat objetua sortu
//        mp1 = MediaPlayer.create(context, R.raw.txalasample2);
//        mp2 = MediaPlayer.create(context, R.raw.txalasample3);
//    }
//    
//    // the method that draws the balls
//    @Override protected void onDraw(Canvas canvas) {
//        //canvas.drawColor(0xFFCCCCCC);     //if you want another background color       
//        
//    	//draw the balls on the canvas
//    	for (ColorBall ball : colorballs) {
//            canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
//         }   
//
//    }
//    
//    // events when touching the screen
//    public boolean onTouchEvent(MotionEvent event) {
//        int eventaction = event.getAction(); 
//        
//        int X = (int)event.getX(); 
//        int Y = (int)event.getY(); 
//
//        switch (eventaction ) { 
//
//        case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
//        	balID = 0;        	
//        	
//        	for (int i = 0; i < colorballs.length; i++) { 
//        		// x eta y kalkulatu
//        		int centerX = colorballs[i].getX();
//        		int centerY = colorballs[i].getY();
//        		
//        		// begiratu ea hatza makilaren gaineran dagoen
//        		if((X < centerX+30 && X > centerX) && ((Y < centerY+150) && (Y > centerY))){
//        			balID = colorballs[i].getID();
//                    break;
//        		}        		
//              }
//             
//             break; 
//
//
//        case MotionEvent.ACTION_MOVE:   // touch drag with the ball   
//        	
//        	// move the balls the same as the finger        	
//            if (balID > 0) {            	
//            	if(Y < 200){
//            		colorballs[balID-1].setX(X-25);
//                	colorballs[balID-1].setY(Y-25);  
//                	jo = true;
//            	}else{            		
//            		if(jo){
//            			if(X>170){
//	            			mp1.start();
//            			}else{
//            				mp2.start();
//            			}
//            			jo = false;
//            		}
//            	}
//            }
//            break; 
//
//        case MotionEvent.ACTION_UP: 
//       		// touch drop - just do things here after dropping        	
////  		 try {
////  			 mp.start();
////		 } catch (Exception e) {
////			 //Log.e(TAG, "error: " + e.getMessage(), e);
////		 }
//
//        } 
//        // redraw the canvas
//        invalidate(); 
//        return true; 
//	
//    }
//}
